//
//  TMGGreenHandViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/13.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGGreenHandViewController.h"
#import "TMGBuyGoldViewController.h"
#import "KLSharePresentController.h"
#import "TMGUserTableViewCell.h"
#import "TMGoldRuleViewController.h"
@interface TMGGreenHandViewController ()
@property(nonatomic, strong) TMGProductModel                     *greenHandModel;
@property(nonatomic, strong) STLabel                     *moneyLable;
@property(nonatomic, strong) NSMutableArray                     *userArray;
@end

@implementation TMGGreenHandViewController
- (void)setGreenHandModel:(TMGProductModel *)greenHandModel{
    _greenHandModel = greenHandModel;
    self.moneyLable.text = [NSString stringWithFormat:@"%@元/克",greenHandModel.price];
    self.moneyLable.attributedText = [self.moneyLable.text st_convertAttributeStringWithKeyWord:greenHandModel.price attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:26]}];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新手特权金";
    [self configSubView];
    [self configBottomView];
    self.view.backgroundColor = self.tableView.backgroundColor;
    self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight - 50);
    [self sendGoodListRequest];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 291)];
    bannerImageView.image = [UIImage imageNamed:@"新手特权金banner"];
    bannerImageView.userInteractionEnabled = YES;
    [header addSubview:bannerImageView];
    
    
    STLabel * greenHandLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 15, UIScreenWidth, 20)
                                                     text:@"新用户首单专享价"
                                                textColor:[UIColor colorWithRed:204/255.0 green:172/255.0 blue:142/255.0 alpha:1]
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [bannerImageView addSubview:greenHandLable];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, greenHandLable.bottom, 200, 65)
                                                     text:@"特权金"
                                                textColor:TM_orangeColor
                                                     font:35
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = UIScreenWidth / 2;

    [bannerImageView addSubview:titleLable];
    
    STLabel * moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(0, titleLable.bottom, UIScreenWidth, 30)
                                                     text:@"--元/克"
                                                textColor:TM_redColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    moneyLable.centerX = UIScreenWidth / 2;
    self.moneyLable = moneyLable;
    moneyLable.attributedText = [moneyLable.text st_convertAttributeStringWithKeyWord:@"248" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:26]}];
    [bannerImageView addSubview:moneyLable];
    
    
    
    STLabel * saleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, moneyLable.bottom  +30, UIScreenWidth, 30)
                                                    text:@"需满90天箱底金，1人仅限购1克"
                                               textColor:[UIColor colorWithRed:209/255.0 green:178/255.0 blue:94/255.0 alpha:1]
                                                    font:13
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    [bannerImageView addSubview:saleLable];
    
    __weak typeof(self) weakSelf =  self;
    STButton * detailButton = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 300, 44)
                                                     title:@"特权金细则"
                                                titleColor:[UIColor colorWithRed:168/255.0 green:104/255.0 blue:36/255.0 alpha:1]
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    
    detailButton.bottom = bannerImageView.height - 60;
    [detailButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedDetailButton];
    }];
    [detailButton setAttributedTitle:[detailButton.currentTitle st_convertAttributeStringWithKeyWord:detailButton.currentTitle attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}] forState:UIControlStateNormal];
    [bannerImageView addSubview:detailButton];
    
    
    STLabel * buyLable = [[STLabel alloc] initWithFrame:CGRectMake(0, bannerImageView.bottom, UIScreenWidth, 44)
                                                     text:@"大家都在买"
                                                textColor:TM_orangeColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    buyLable.backgroundColor = self.tableView.backgroundColor;
    [header addSubview:buyLable];
    
    header.height = buyLable.bottom;

    self.tableView.tableHeaderView = header;
}
- (void)configBottomView{
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    bootomView.top = [TMUtils tabBarTop];
    [bootomView st_showTopShadow];
    [self.view addSubview:bootomView];
    
    __weak typeof(self) weakSelf =  self;
    self.tableView.height = self.tableView.height - 44;
    STButton * outButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth/2, 50)
                                                     title:@"立即购买"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:TM_orangeColor
                                           backgroundImage:nil
                                                     image:nil];
    outButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [outButton st_showRightLine:outButton.height];
    [outButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedBuyGoldButton];
    }];
    [bootomView addSubview:outButton];
    
    STButton * tijintButton = [[STButton alloc] initWithFrame:CGRectMake(outButton.right, 0, UIScreenWidth/2, 50)
                                                        title:@"分享给好友"
                                                   titleColor:TM_orangeColor
                                                    titleFont:16
                                                 cornerRadius:0
                                              backgroundColor:TM_backgroundColor
                                              backgroundImage:nil
                                                        image:nil];
    tijintButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [tijintButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedShareGoldButton];
    }];
    [bootomView addSubview:tijintButton];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.userArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGUserTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGUserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGUserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.userArray[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelectedDetailButton{
    [self.navigationController pushViewController:[TMGoldRuleViewController new] animated:YES];
}
- (void)onSlectedBuyGoldButton{
    TMGBuyGoldViewController * vc = [[TMGBuyGoldViewController alloc] initWithGoldType:TMGBuyGoldTypeGreenHand];
    NSDictionary * dic = self.greenHandModel.mj_keyValues;
    TMGProductDetailModel * model = [TMGProductDetailModel mj_objectWithKeyValues:dic];
    model.price = self.greenHandModel.price;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onSlectedShareGoldButton{
    KLSharePresentController * vc = [[KLSharePresentController alloc] initWithHandle:^(NSString *chosedIde) {
        if ([chosedIde isEqualToString:@"朋友圈"]) {
            [self shareFriendCircle];
        }else{
            [self shareToGoodFriend];
        }
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --share
//微信朋友圈
- (void)shareFriendCircle{
    
    //    SendMessageToWXReq  *req = [SendMessageToWXReq new];
    //    req.text = wechatShareDesc;
    //    req.bText = YES;
    //    req.scene = WXSceneSession;
    //    req.scene = WXSceneTimeline;
    //    [WXApi sendReq:req];
    
    NSString * url = [NSString stringWithFormat:@"http://jshjkd.com/gold/public/home/web/friend?uid=%@",[STUserManger defult].userID];
    SendMessageToWXReq  *req = [SendMessageToWXReq new];
    req.scene = WXSceneTimeline;
    WXMediaMessage *urlMessage = [WXMediaMessage message];
    urlMessage.title = wechatShareDesc;
    //分享标题 urlMessage.description = kLinkDescription;//分享描述
    [urlMessage setThumbImage:[UIImage imageNamed:@"logo_release"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
    //创建多媒体对象
    WXWebpageObject *webObj = [WXWebpageObject object];
    webObj.webpageUrl = url;//分享链接 //完成发送对象实例
    urlMessage.mediaObject = webObj;
    req.message = urlMessage; //发送分享信息
    [WXApi sendReq:req];
    
    
}
//微信好友
- (void)shareToGoodFriend{
    NSString * url = [NSString stringWithFormat:@"http://jshjkd.com/gold/public/home/web/share?tid=%@",[STUserManger defult].userID];
    SendMessageToWXReq  *req = [SendMessageToWXReq new];
    req.scene = WXSceneSession;
    WXMediaMessage *urlMessage = [WXMediaMessage message];
    urlMessage.title = wechatShareDesc;
    //分享标题 urlMessage.description = kLinkDescription;//分享描述
    [urlMessage setThumbImage:[UIImage imageNamed:@"logo_release"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
    //创建多媒体对象
    WXWebpageObject *webObj = [WXWebpageObject object];
    webObj.webpageUrl = url;//分享链接 //完成发送对象实例
    urlMessage.mediaObject = webObj;
    req.message = urlMessage; //发送分享信息
    [WXApi sendReq:req];
}
#pragma mark --NetWork Method
- (void)sendGoodPeopleBuyReqeust{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/buyGoodsNumbers"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.greenHandModel.goodiID.description.length) {
        [dic setObject:self.greenHandModel.goodiID forKey:@"gid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 self.userArray = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGBuyUserModel * model = [TMGBuyUserModel mj_objectWithKeyValues:modelDic];
                                                     [self.userArray addObject:model];
                                                 }
                                                 [self.tableView reloadData];


                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendGoodListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.model.productID.description.length) {
        [dic setObject:self.model.productID forKey:@"cate"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array =  responseObject[@"data"];
                                               //  self.dataSouce = [NSMutableArray new];
                                                 
                                                 NSDictionary * dataDic = array.firstObject;
                                                 TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:dataDic];
                                                 self.greenHandModel = model;
                                                 [self sendGoodPeopleBuyReqeust];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
